﻿using CodeSystem.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;

namespace CodeSystem.App_Start
{
    public class BaseAuthorize : AuthorizeAttribute
    {
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            var authorization = actionContext.Request.Headers.Authorization;


            var content = actionContext.Request.Properties["MS_HttpContext"] as HttpContextBase;
            var token = content.Request.Headers["token"];

            string setting= ConfigurationManager.AppSettings["token"];
            if (string.IsNullOrEmpty(token)||token!=setting)
            {
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized) 
                {StatusCode=HttpStatusCode.Unauthorized, ReasonPhrase = "未授权" }; ;
                base.OnAuthorization(actionContext);
            }
        }
    }
}